package com.breakfast.fun.ui.user.login;

import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.breakfast.fun.R;
import com.breakfast.fun.base.BaseActivity;
import com.breakfast.fun.util.T;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ForgetActivity extends BaseActivity<ForgetPresenter, ForgetModel> implements ForgetContract.View {


    @Bind(R.id.iv_forget_back)
    ImageView mIvForgetBack;
    @Bind(R.id.et_forget_mobile)
    EditText mEtForgetMobile;
    @Bind(R.id.tv_forget_get_code)
    TextView mTvForgetGetCode;
    @Bind(R.id.et_forget_code)
    EditText mEtForgetCode;
    @Bind(R.id.et_forget_pwd)
    EditText mEtForgetPwd;
    @Bind(R.id.et_forget_pwd_again)
    EditText mEtForgetPwdAgain;
    @Bind(R.id.ll_forget)
    LinearLayout mLlForget;
    @Bind(R.id.btn_forget_submit)
    Button mBtnForgetSubmit;
    private int time = 60;
    private Timer timer = new Timer();
    private TimerTask task;

    @Override
    public int getLayoutId() {
        return R.layout.activity_forget;
    }

    @Override
    public void initView() {


        mTvForgetGetCode.setOnClickListener(v -> {
            if (mEtForgetMobile.getText().length() != 11) {
                T.show("手机格式不正确");
                return;
            }
            presenter.getCode(mEtForgetMobile.getText().toString());
        });

        mBtnForgetSubmit.setOnClickListener(v -> {
            if (mEtForgetMobile.getText().length() == 0
                    || mEtForgetCode.getText().length() == 0
                    || mEtForgetPwd.getText().length() == 0
                    || mEtForgetPwdAgain.getText().length() == 0) {
                T.show("输入不完整,请检查");
                return;
            }
            if (!mEtForgetPwdAgain.getText().toString().equals(mEtForgetPwd.getText().toString())) {
                T.show("两次输入的密码不一致");
                return;
            }
            presenter.resetPwd(mEtForgetMobile.getText().toString(), mEtForgetCode.getText().toString(), mEtForgetPwd.getText().toString());
        });

        mIvForgetBack.setOnClickListener(v -> {
            finish();
        });

        mEtForgetMobile.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s) || s.length() < 11) {
                    mTvForgetGetCode.setBackgroundResource(R.color.gray_silver);
                    mTvForgetGetCode.setClickable(false);
                    mEtForgetCode.setEnabled(false);
                } else if (!TextUtils.isEmpty(s) || s.length() == 11) {
                    mTvForgetGetCode.setBackgroundResource(R.drawable.selector_btn);
                    mTvForgetGetCode.setClickable(true);
                    mEtForgetCode.setEnabled(true);
                }
            }
        });
    }

    @Override
    public void sendCodeSuccess() {
        T.show("发送成功");
        new SendMessage().execute();
    }

    @Override
    public void resetPwdSuccess() {
        T.show("重置成功");
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    class SendMessage extends AsyncTask<Integer, Integer, Integer> {

        @Override
        protected Integer doInBackground(Integer... params) {

            return 1;
        }

        @Override
        protected void onPostExecute(Integer result) {
            mTvForgetGetCode.setEnabled(false);
            mTvForgetGetCode.setBackgroundResource(R.color.gray_silver);
            task = new TimerTask() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable() { // UI thread
                        @Override
                        public void run() {
                            if (time <= 0) {
                                // 当倒计时小余=0时记得还原图片，可以点击
                                mTvForgetGetCode.setEnabled(true);
                                mTvForgetGetCode.setBackgroundResource(R.drawable.selector_btn);
                                mTvForgetGetCode.setTextColor(Color.WHITE);
                                mTvForgetGetCode.setText("获取验证码");
                                task.cancel();
                            } else {
                                if (mTvForgetGetCode == null) {
                                    return;
                                }
                                mTvForgetGetCode.setText(time + "秒后重试");
                                mTvForgetGetCode.setTextColor(Color.rgb(125, 125, 125));
                            }
                            time--;
                        }
                    });
                }
            };

            time = 60;
            timer.schedule(task, 0, 1000);
        }
    }

    @Override
    public void finish() {
        if (task != null) {
            task.cancel();
        }
        super.finish();
    }
}
